import 'package:dio/dio.dart';
import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/constans/storage.dart';
import 'package:safetymonitor/app/core/env/env.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/core/utils/log_util.dart';
import 'package:safetymonitor/app/core/utils/network.dart';
import 'package:safetymonitor/app/core/utils/storage_util.dart';

class NetworkManager {

  /// 当前网络环境
  static String netWorkInfo = NetWorkInfo.extranet.name;

  /// 内网地址
  static void switchIntranetAddress() {
    if(netWorkInfo == NetWorkInfo.extranet.name) {
      AppToast.showSuccess('外网切换内网成功,请重新尝试',title: '切换网络');
    }
    // 内网
    Interface.mainBaseUrl = 'http://172.13.200.33:30000';
    Interface.fileUrl = 'http://192.168.50.67:9090';
    //标绘地图地址（内网地址）
    Interface.plotMapAddress = 'http://10.1.31.111:30012';
    AppLogger.log('---------------服务器地址：内网');
    saveNetWorkInfo(NetWorkInfo.intranet);
  }

  static void switchExtranetAddress() {
    //外网
    Interface.mainBaseUrl = 'http://219.145.189.219:30000';
    Interface.fileUrl = 'http://219.145.189.219:9090';
    //标绘地图地址（外网地址）
    Interface.plotMapAddress = 'http://219.145.189.219:30009';
    AppLogger.log('---------------服务器地址：外网');
    saveNetWorkInfo(NetWorkInfo.extranet);
  }

  /// 保存当前内网/外网信息
  static saveNetWorkInfo(NetWorkInfo netWork) {
    netWorkInfo = netWork.name;
    StorageUtil.setString(StorageConstants.netWorkInfo, netWork.name);
  }

  /// 获取当前内网/外网信息
  static String getNetWorkInfo() {
    // return NetWorkInfo.extranet.name;
    return StorageUtil.getString(StorageConstants.netWorkInfo) ?? NetWorkInfo.extranet.name;
  }



  /// 检查网络类型
  /// 如果网络类型为wifi，则判断是否为内网，是内网则切换内网，否则切换外网
  static Future<bool> checkNetworkType() async {

    if(DartDefines.appInstallChannel != AppInstallChannel.releaseservice) {
      return true;
    }

    if(NetworkUtil.network == NetworkType.wifi) {
      // 创建Dio实例，并设置全局选项
      final dio = Dio(BaseOptions(
        connectTimeout: const Duration(milliseconds: 1000), // 连接超时时间（毫秒）
        sendTimeout: const Duration(milliseconds: 1000),    // 发送超时时间（毫秒）
        receiveTimeout: const Duration(milliseconds: 1000), // 接收超时时间（毫秒）
      ));

      try {
        //  发送GET请求
        final  response = await dio.get('http://172.13.200.100:30000/system/company-system-device/getInOrOutNet');
        if(response.statusCode == 200) {
          // AppToast.showSuccess('访问内网成功,已切换到内网,当前网络环境为：${NetworkUtil.network.name}',duration: const Duration(seconds: 2));
          AppLogger.log('访问内网成功,已切换到内网,当前网络环境为：${NetworkUtil.network.name}');
          switchIntranetAddress();
          return true;
        } else {
          // AppToast.showError('访问内网失败');
          AppLogger.log('访问内网失败');
          saveNetWorkInfo(NetWorkInfo.extranet);
          return true;
        }
      } catch (e) {
        AppLogger.log('--------------访问内网失败: $e');
        saveNetWorkInfo(NetWorkInfo.extranet);
        return true;
        // AppToast.showError('访问内网失败,当前网络环境为：${NetworkUtil.network.name}，已切换到外网环境');
      }
    } else {
      // AppToast.showError('当前网络环境为：${NetworkUtil.network.name}，已切换到外网环境');
      AppLogger.log('当前网络环境为：${NetworkUtil.network.name}，已切换到外网环境');
      saveNetWorkInfo(NetWorkInfo.extranet);
      return true;
    }

  }



}


/// 当前系统环境
enum NetWorkInfo {
  /// 内网
  intranet,
  /// 外网
  extranet
}
